package com.admin.system.controller;

import com.admin.model.base.PageResult;
import com.admin.model.dto.TypeMappingDto;
import com.admin.model.dto.TypeMappingDto;
import com.admin.model.pojo.Job;
import com.admin.model.pojo.TypeMapping;
import com.admin.system.service.TypeMappingService;
import com.admin.system.service.mapstruct.TypeMappingMapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.groups.Default;
import java.util.List;
import java.util.Set;

/**
 * (Job)表控制层
 *
 * @author caixiaofeng
 * @since 2021-03-13 21:49:26
 */
@Api(tags = "类型映射")
@RestController
@RequestMapping("typeMapping")
public class TypeMappingController {
    @Resource
    private TypeMappingService typeMappingService;

    @ApiOperation("创建")
    @ApiOperationSupport(ignoreParameters = {"id", "createTime", "updateTime", "createdBy", "updatedBy", "createdUser", "updatedUser"})
    @PreAuthorize("hasAnyAuthority('mapping:add')")
    @PostMapping
    public ResponseEntity<Object> create(@Validated({TypeMappingDto.Create.class, Default.class}) @RequestBody TypeMappingDto typeMappingDto) {
        typeMappingService.create(typeMappingDto);
        return ResponseEntity.ok("创建成功");
    }

    @ApiOperation("id查询")
    @PreAuthorize("hasAnyAuthority('mapping:list')")
    @GetMapping("/{id}")
    public ResponseEntity<Object> findById(@PathVariable Long id) {
        TypeMappingDto typeMappingDto = typeMappingService.findById(id);
        return ResponseEntity.ok(typeMappingDto);
    }


    @ApiOperation("ids删除")
    @PreAuthorize("hasAnyAuthority('mapping:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteByIds(@RequestBody Set<Long> ids) {
        typeMappingService.deleteByIds(ids);
        return ResponseEntity.ok("删除成功");
    }

    @ApiOperation("条件查询")
    @ApiOperationSupport(ignoreParameters = {"createdUser", "updatedUser"})
    @PreAuthorize("hasAnyAuthority('mapping:list')")
    @PostMapping("/search")
    public ResponseEntity<Object> findAll(@RequestParam(defaultValue = "1") int pageNum,
                                          @RequestParam(defaultValue = "10") int pageSize,
                                          @RequestParam(defaultValue = "-create_time") String sort,
                                          @RequestBody(required = false) TypeMappingDto typeMappingDto) {
        PageResult pageResult = typeMappingService.queryAll(typeMappingDto, pageNum, pageSize, sort);
        return ResponseEntity.ok(pageResult);
    }

    @ApiOperation("id更新")
    @ApiOperationSupport(ignoreParameters = {"createTime", "updateTime", "createdBy", "updatedBy", "createdUser", "updatedUser"})
    @PreAuthorize("hasAnyAuthority('mapping:edit')")
    @PutMapping
    public ResponseEntity<Object> updateById(@Validated({TypeMappingDto.Update.class, Default.class}) @RequestBody TypeMappingDto TypeMappingDto) {
        typeMappingService.update(TypeMappingDto);
        return ResponseEntity.ok("更新成功");
    }

}